package controllers

import (
	"github.com/astaxie/beego"
	_"github.com/go-sql-driver/mysql"
	"web/models"
)

type TestModelController struct {
	beego.Controller
}

//数据库表对应的实体类
/*type UserInfo struct {
	Id int64
	Username string
	Password string
}
*/
func (c *TestModelController) Get() {
	/*//设置数据库的链接
	orm.RegisterDataBase("default","mysql","root:root@tcp(localhost:3306)/test?charset=utf8",30)
	//设置具体操作的数据库中的表
	orm.RegisterModel(new(UserInfo))

	o := orm.NewOrm()*/

	//增
	/*	user := UserInfo{Username :"zhangsan",Password:"123"}
	id, err :=o.Insert(&user)*/

	//改
	/*	user := UserInfo{Id:1,Username :"lisi",Password:"123"}
	id, err :=o.Update(&user)

	c.Ctx.WriteString(fmt.Sprintf("id: %d err: %v",id, err))
	*/

	//查
	/*	user := UserInfo{Id:1}
	o.Read(&user)

	c.Ctx.WriteString(fmt.Sprintf("user info: %v",user))*/

	//原生查询
/*	var maps []orm.Params
	o.Raw("SELECT * FROM user_info").Values(&maps)

	for _, v := range maps {
		c.Ctx.WriteString(fmt.Sprintf("user info:%v",v))
	}*/

	//采用queryBuilder方式进行读取
/*	var users []UserInfo

	qb,_ :=orm.NewQueryBuilder("mysql")

	qb.Select("password").From("user_info").Where("username=?").Limit(1)

	sql := qb.String()
	o.Raw(sql,"lisi").QueryRows(&users)
	c.Ctx.WriteString(fmt.Sprintf("user info:%v",users))
*/


	user :=models.UserInfo{Username:"wangwu", Password:"56789"}
	models.AddUser(&user)

	c.Ctx.WriteString("call Model Success")

}

func (c *TestModelController) Post() {

}